<?php
/**
 * 云上绵州线下商户
 */

use app\config\Db;
use Core\Result\Result;
use Core\Common\Common;

$sign = "jYylSFE585fEE84KLIUYhgsdf@";
/**
 * 商家收款码展示 TODO
 * @param $data
 * @return array
 * @throws
 */
function merchantPaymentCode($data)
{
    $sign = $GLOBALS['sign'];
    $user_id = Common::getUser_id($data);
    $db = new Db();
    $merchantInfo = $db->find("select `merchant_id`,`merchant_name`,`doorboard_img_id` from `ysmz_merchant_offline` where `user_id`=$user_id and `is_delete` = 0 and `state` = 2");
    if (!$merchantInfo) Result::error("当前账号未关联线下商户，暂不可用");
    $fileInfo = $db->find("select file_id,storage,`domain`,file_path from `ysmz_file` where `file_id`={$merchantInfo['doorboard_img_id']} limit 1");
    $merchantInfo["doorboard_img"] = Common::fileUrl($fileInfo);
    $merchantInfo["sign"] = hash_hmac('md5',$merchantInfo["merchant_id"],$sign);
    Result::success($merchantInfo);
}
/**
 * 线下商家分类列表
 */
function merchantCategoryList($data)
{
    $limit = $data["limit"];
    $page = $data["page"];
    $db = new Db();
    $categoryInfo = $db->query("SELECT `category_id`,`category_name`,`img_id`,`title` FROM `ysmz_merchant_offline_category` WHERE `is_hide` = 0 and is_delete = 0 ORDER BY sort desc limit $page,$limit");
    foreach ($categoryInfo as $k => $v) {
        $fileInfo = $db->find("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` =$v[img_id]");
        $categoryInfo[$k]["category_img"] = Common::fileUrl($fileInfo);
    }
    Result::success($categoryInfo);
}
/**
 * 线下商家品牌列表
 */
function merchantMakeList($data)
{
    $limit = $data["limit"];
    $page = $data["page"];
    $db = new Db();
    $categoryInfo = $db->query("SELECT `brand_id`,`category_id`,`name`,`img_id`,`imgs`,`title`,`img_ids`,`description`,`keywords` FROM `ysmz_merchant_offline_brand` WHERE `is_hide` = 0 and is_delete = 0 ORDER BY sort desc limit $page,$limit");
    foreach ($categoryInfo as $k => $v) {
        $fileInfo = $db->find("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` =$v[img_id]");
        $categoryInfo[$k]["brand_img"] = Common::fileUrl($fileInfo);
        $backgroundInfo = $db->find("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` =$v[imgs]");
        $categoryInfo[$k]["background_img"] = Common::fileUrl($backgroundInfo);
        $categoryInfo[$k]["category_name"] = "";
        if(!empty($v["category_id"])){
            $cInfo = $db->find("select `category_name` from `ysmz_merchant_offline_category` where `category_id` =$v[category_id]");
            $categoryInfo[$k]["category_name"] = $cInfo["category_name"];
        }
        $scene_imgs = $db->query("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` in($v[img_ids])");
        foreach ($scene_imgs as $kk => $vv) {
            $categoryInfo[$k]["brandImgs"][] = Common::fileUrl($vv);
        }
    }
    Result::success($categoryInfo);
}
/**
 * 线下分类门店
 */
function offlineClassifiedStores($data)
{
    merchantList($data);
}
/**
 * 线下商家列表
 */
function merchantList($data)
{
    $lon = $data["lon"] ?? "104.75708";//经度
    $lat = $data["lat"] ?? "31.45498";//纬度
    $limit = $data["limit"];//纬度
    $page = $data["page"];//纬度
    $category_id = $data["category_id"];
    $brand_id = $data["brand_id"];
    $merchant_name = $data["merchant_name"];
    if(($category_id && !is_numeric($category_id)) || ($brand_id && !is_numeric($brand_id))){
        Result::error("参数错误");
    }
    $where = "";
    if(!empty($category_id)){
        $where .= " and `category_id` = $category_id";
    }
    if(!empty($brand_id)){
        $where .= " and `brand_id` = $brand_id";
    }
    if(!empty($merchant_name)){
        $where .= " and `merchant_name` like '%$merchant_name%'";
    }
    $db = new Db();
    $offlineInfo = $db->query("SELECT `merchant_id`,`merchant_name`,`merchant_simple_name`,`cont_name`,`cont_phone`,`doorboard_img_id`,`cust_addr`,ROUND(6371 * acos(cos(radians($lat)) * cos(radians(latitude)) * cos(radians(longitude) - radians($lon)) + sin(radians($lat)) * sin(radians(latitude))), 2) AS distance_km FROM ysmz_merchant_offline WHERE `state` = 1 $where and latitude BETWEEN $lat - 1 AND $lat + 1 AND longitude BETWEEN $lon - 2 AND $lon + 2 ORDER BY distance_km ASC LIMIT $page,$limit");
    foreach ($offlineInfo as $k => $v) {
        if($v["distance_km"] <0.1){
            $offlineInfo[$k]["distance_km"] = "100米以内";
        }else{
            $offlineInfo[$k]["distance_km"] = $v["distance_km"]."KM";
        }
        $fileInfo = $db->find("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` =$v[doorboard_img_id]");
        $offlineInfo[$k]["doorboard_img"] = Common::fileUrl($fileInfo);
    }
    Result::success($offlineInfo);
}
/**
 * 通过订单查询线下商家列表
 */
function merchantOrderList($data)
{
    $lon = $data["longitude"] ?? "104.75708";//经度
    $lat = $data["latitude"] ?? "31.45498";//纬度
    $shop_id = $data["shop_id"];
    $db = new Db();
    $offlineInfo = $db->query("SELECT `merchant_id`,`merchant_name`,`cont_phone`,`cust_addr`,ROUND(6371 * acos(cos(radians($lat)) * cos(radians(latitude)) * cos(radians(longitude) - radians($lon)) + sin(radians($lat)) * sin(radians(latitude))), 2) AS distance_km FROM ysmz_merchant_offline WHERE `shop_id` = $shop_id and `state` = 1 and latitude BETWEEN $lat - 2 AND $lat + 2 AND longitude BETWEEN $lon - 3 AND $lon + 3 ORDER BY distance_km ASC");
    foreach ($offlineInfo as $k => $v) {
        if($v["distance_km"] <0.1){
            $offlineInfo[$k]["distance_km"] = "100米以内";
        }else{
            $offlineInfo[$k]["distance_km"] = $v["distance_km"]."KM";
        }
    }
    return $offlineInfo;
}
/**
 * 查询店铺绑定商家数量
 */
function merchantCount($data)
{
    $shop_id = $data["shop_id"];
    $db = new Db();
    $offlineInfo = $db->find("SELECT count(merchant_id) as count FROM `ysmz_merchant_offline` WHERE `shop_id` = $shop_id and `state` = 1");
    return $offlineInfo["count"];
}
/**
 * 线下商家详情
 */
function merchantInfo($data)
{
    $lon = $data["lon"] ?? "104.75708";//经度
    $lat = $data["lat"] ?? "31.45498";//纬度
    $merchant_id = $data["merchant_id"];
    $db = new Db();
    $offlineInfo = $db->find("SELECT `merchant_id`,`merchant_name`,`merchant_simple_name`,`cont_name`,`cont_phone`,`doorboard_img_id`,`cust_addr`,`info`,`scene_imgs`,`longitude`,`latitude`,ROUND(6371 * acos(cos(radians($lat)) * cos(radians(latitude)) * cos(radians(longitude) - radians($lon)) + sin(radians($lat)) * sin(radians(latitude))), 2) AS distance_km FROM ysmz_merchant_offline WHERE `state` = 1 and `merchant_id` = $merchant_id and latitude BETWEEN $lat - 1 AND $lat + 1 AND longitude BETWEEN $lon - 2 AND $lon + 2 ORDER BY distance_km ASC");
    if($offlineInfo["distance_km"] <0.1){
        $offlineInfo["distance_km"] = "100米以内";
    }else{
        $offlineInfo["distance_km"] = $offlineInfo["distance_km"]."KM";
    }
    $fileInfo = $db->find("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` =$offlineInfo[doorboard_img_id]");
    $offlineInfo["doorboardImg"] = Common::fileUrl($fileInfo);
    $scene_imgs = $db->query("select `file_id`,`storage`,`domain`,`file_path` from `ysmz_file` where `file_id` in($offlineInfo[scene_imgs])");
    foreach ($scene_imgs as $k => $v) {
        $offlineInfo["sceneImgs"][] = Common::fileUrl($v);
    }
    Result::success($offlineInfo);
}